/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cloudptt.wp.receiver;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.KeyEvent;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.ui.main.MainActivity;


/**
 * Receives broadcasted intents. In particular, we are interested in the
 * android.media.AUDIO_BECOMING_NOISY and android.intent.action.MEDIA_BUTTON
 * intents, which is broadcast, for example, when the user disconnects the
 * headphones. This class works because we are declaring it in a
 * &lt;receiver&gt; tag in AndroidManifest.xml.
 */
@SuppressLint("InlinedApi")
public class PTTIntentReceiver extends BroadcastReceiver {
	private static final String LOG_TAG = "PTTIntentReceiver";
	private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
//	public final static String ACTION_SHOW = "com.cloudptt.wp.show";
	private Context mContext;
	public  static final  int KICK_EVENT = 79;


	@Override
	public void onReceive(Context context, Intent intent) 
	{
		mContext = context;
		String action = intent.getAction();
		Log.d(LOG_TAG, "onReceive action = " + action);
		if(action == null)
		{
			return;
		}
		if (ACTION.equals(action)) {

//第一种方式：根据包名
            PackageManager packageManager = context.getPackageManager();
            Intent mainIntent = packageManager.getLaunchIntentForPackage("com.cloudptt.wp");
            mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mainIntent);
            context.startService(mainIntent);


//第二种方式：指定class类，跳转到相应的Acitivity
//			Intent mainIntent = new Intent(context, MainActivity.class);
//			/**
//			 * Intent.FLAG_ACTIVITY_NEW_TASK
//			 * Intent.FLAG_ACTIVITY_CLEAR_TOP
//			 */
//			mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			context.startActivity(mainIntent);
//            context.startService(mainIntent);
		}
		else if (action.equals(Intent.ACTION_MEDIA_BUTTON))
		{
			KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(
					Intent.EXTRA_KEY_EVENT);

			// 获得按键字节码  
			int keyCode = keyEvent.getKeyCode();  
			// 按下 / 松开 按钮  
			int keyAction = keyEvent.getAction();  
			// 获得事件的时间  
			long downtime = keyEvent.getEventTime();  

			Log.e(LOG_TAG, "11 keyaction "  + keyAction +"  keycode" +keyEvent.getKeyCode() +" downtime :"+downtime);
			switch (keyEvent.getKeyCode()) {

			case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
				// context.startService(new
				// Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
				break;
			case KeyEvent.KEYCODE_MEDIA_PLAY:
				// context.startService(new Intent(MusicService.ACTION_PLAY));
				break;
			case KeyEvent.KEYCODE_MEDIA_PAUSE:
				// context.startService(new Intent(MusicService.ACTION_PAUSE));
				break;
			case KeyEvent.KEYCODE_MEDIA_STOP:
				// context.startService(new Intent(MusicService.ACTION_STOP));
				break;
			case KeyEvent.KEYCODE_MEDIA_NEXT:
				// context.startService(new Intent(MusicService.ACTION_SKIP));
				break;
			case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
				// TODO: ensure that doing this in rapid succession actually
				// plays the
				// previous song
				// context.startService(new Intent(MusicService.ACTION_REWIND));
				break;
			case KeyEvent.KEYCODE_HEADSETHOOK:

//				if(keyAction==1)
//				{
//					lockScreen();
//				}

				Log.i(LOG_TAG, "we want to KEYCODE_HEADSETHOOK "+ keyEvent.getKeyCode());
				break;
			default:

				Log.i(LOG_TAG, "we want to know "+ keyEvent.getKeyCode());
				break;

			}

		} else if (action.equals("android.intent.action.BOOT_COMPLETED")) {
			Log.d(LOG_TAG, "android.intent.action.BOOT_COMPLETED");
			Intent ootStartIntent=new Intent(context, MainActivity.class);
			ootStartIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
			context.startActivity(ootStartIntent); 
		}
//		else if (action.equals(ACTION_SHOW)) {
//			Log.d(LOG_TAG, "lightControl com.cloudptt.wp.show");
////			bring2Front(context);
////			lockScreen();
//
//			if(isAppBackgroud(context)) {
//				Log.d(LOG_TAG, "lightControl com.cloudptt.wp.show in");
//				Intent ootStartIntent = new Intent(context, MainActivity.class);
//				ootStartIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//				ootStartIntent.setAction(Intent.ACTION_MAIN);
//				ootStartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
////			ootStartIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//				context.startActivity(ootStartIntent);
//			}
////
//		}
	}

//	public static boolean isAppBackgroud(Context context) {
//		if (context != null) {
//			String packName = context.getPackageName();
//			List<ActivityManager.RunningTaskInfo> rTasks = getRunningTask(context, 1);
//			ActivityManager.RunningTaskInfo task = rTasks.get(0);
//			return !packName.equalsIgnoreCase(task.topActivity.getPackageName());
//		}
//		return false;
//	}
//	public static List<ActivityManager.RunningTaskInfo> getRunningTask(Context context, int num) {
//		if (context != null) {
//			ActivityManager am = (ActivityManager) context
//					.getSystemService(Context.ACTIVITY_SERVICE);
//			List<ActivityManager.RunningTaskInfo> rTasks = am.getRunningTasks(1);
//			return rTasks;
//		}
//		return null;
//	}

}
